package controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.FundDAO;
import model.FundPriceHistoryDAO;
import model.Model;

import org.mybeans.dao.DAOException;

import util.Bean2XML;
import databean.FundPriceHistoryBean;

public class GetFundPriceHistoryAction extends Action{
    private FundPriceHistoryDAO historyDAO;
    private FundDAO fundDAO;
	@Override
	public String getName() {
		return "get-history.do";
	}
    public GetFundPriceHistoryAction(Model model){
    	this.historyDAO=model.getFundPriceHistoryDAO();
    	this.fundDAO=model.getFundDAO();
    }
	@Override
	public String perform(HttpServletRequest request,HttpServletResponse response) {
		long fundID=Long.parseLong(request.getParameter("fundID"));
		List<FundPriceHistoryBean> hs=null;
		try {
			 hs=historyDAO.lookup(fundID);
		} catch (DAOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		//TODO sort by date then retrieve first 50 or something, only useful for real...
		
		String rs=Bean2XML.FundPriceHistorys(hs, fundDAO);
		response.setContentType("application/xml"); 
		response.setHeader("Content-disposition","attachment; filename=historys.xml"); 
		try {
			PrintWriter out;
			out = response.getWriter();
			out.print(rs);
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "empty.jsp";
	}

}
